﻿using UnityEngine;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {

        [CtxMenuItem("Renderer/Clipboard/Paste/VertexData/Vertices"
            ,"Pastes vertex data from one renderer to another.",
             ContextFlag.PasteFunction, ContextRequirements.CanPasteVertex, 001)]
        public static void RendererPasteVertices()
        {
            var item = SkinnContext.GetContextItem();
            if (item && SkinnEx.IsNullOrNotInAScene(item.transform)) return;
            var context = item.GetComponent<Renderer>();
            if (SkinnEx.IsNullOrNotVailid(context)) return;

            var copyContext = (Renderer)SkinnContext.CopyRef.Asset;
            if (!copyContext) return;
            var copyMesh = copyContext.GetSharedMesh();

            var mesh = context.GetSharedMesh().Clone() as Mesh;

            if (copyMesh.CanApplyVertices(mesh))
            {
                mesh.vertices = copyMesh.vertices;
                SkinnContext.UndoRecord(context, "Paste Vertex Data");
                context.SetSharedMesh(mesh);
            }
            else
            {
                SkinnEx.Release(mesh);
                Debug.LogWarning("Paste Vertex Data Failed.");
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Clipboard/Paste/VertexData/Normals&Tangents"
           , "Pastes vertex data from one renderer to another.",
            ContextFlag.PasteFunction, ContextRequirements.CanPasteVertex, 001)]
        public static void RendererPasteNormalsTangents()
        {
            var item = SkinnContext.GetContextItem();
            if (item && SkinnEx.IsNullOrNotInAScene(item.transform)) return;
            var context = item.GetComponent<Renderer>();
            if (SkinnEx.IsNullOrNotVailid(context)) return;

            var copyContext = (Renderer)SkinnContext.CopyRef.Asset;
            if (!copyContext) return;
            var copyMesh = copyContext.GetSharedMesh();

            var mesh = context.GetSharedMesh().Clone() as Mesh;

            if (copyMesh.CanApplyVertices(mesh))
            {
                mesh.normals = copyMesh.normals;
                mesh.tangents = copyMesh.tangents;
                SkinnContext.UndoRecord(context, "Paste Vertex Data");
                context.SetSharedMesh(mesh);
            }
            else
            {
                SkinnEx.Release(mesh);
                Debug.LogWarning("Paste Vertex Data Failed.");
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Clipboard/Paste/VertexData/Colors"
            , "Pastes vertex data from one renderer to another.",
             ContextFlag.PasteFunction, ContextRequirements.CanPasteVertex, 001)]
        public static void RendererPasteColors()
        {
            var item = SkinnContext.GetContextItem();
            if (item && SkinnEx.IsNullOrNotInAScene(item.transform)) return;
            var context = item.GetComponent<Renderer>();
            if (SkinnEx.IsNullOrNotVailid(context)) return;

            var copyContext = (Renderer)SkinnContext.CopyRef.Asset;
            if (!copyContext) return;
            var copyMesh = copyContext.GetSharedMesh();

            var mesh = context.GetSharedMesh().Clone() as Mesh;

            if (copyMesh.CanApplyVertices(mesh))
            {
                mesh.colors = copyMesh.colors;
                SkinnContext.UndoRecord(context, "Paste Vertex Data");
                context.SetSharedMesh(mesh);
            }
            else
            {
                SkinnEx.Release(mesh);
                Debug.LogWarning("Paste Vertex Data Failed.");
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Clipboard/Paste/VertexData/Colors32"
         , "Pastes vertex data from one renderer to another.",
          ContextFlag.PasteFunction, ContextRequirements.CanPasteVertex, 001)]
        public static void RendererPasteColors32()
        {
            var item = SkinnContext.GetContextItem();
            if (item && SkinnEx.IsNullOrNotInAScene(item.transform)) return;
            var context = item.GetComponent<Renderer>();
            if (SkinnEx.IsNullOrNotVailid(context)) return;

            var copyContext = (Renderer)SkinnContext.CopyRef.Asset;
            if (!copyContext) return;
            var copyMesh = copyContext.GetSharedMesh();

            var mesh = context.GetSharedMesh().Clone() as Mesh;

            if (copyMesh.CanApplyVertices(mesh))
            {
                mesh.colors32 = copyMesh.colors32;
                SkinnContext.UndoRecord(context, "Paste Vertex Data");
                context.SetSharedMesh(mesh);
            }
            else
            {
                SkinnEx.Release(mesh);
                Debug.LogWarning("Paste Vertex Data Failed.");
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Clipboard/Paste/VertexData/UVs"
        , "Pastes vertex data from one renderer to another.",
         ContextFlag.PasteFunction, ContextRequirements.CanPasteVertex, 001)]
        public static void RendererPasteUV0()
        {
            var item = SkinnContext.GetContextItem();
            if (item && SkinnEx.IsNullOrNotInAScene(item.transform)) return;
            var context = item.GetComponent<Renderer>();
            if (SkinnEx.IsNullOrNotVailid(context)) return;

            var copyContext = (Renderer)SkinnContext.CopyRef.Asset;
            if (!copyContext) return;
            var copyMesh = copyContext.GetSharedMesh();

            var mesh = context.GetSharedMesh().Clone() as Mesh;

            if (copyMesh.CanApplyVertices(mesh))
            {
                mesh.uv = copyMesh.uv;
                SkinnContext.UndoRecord(context, "Paste Vertex Data");
                context.SetSharedMesh(mesh);
            }
            else
            {
                SkinnEx.Release(mesh);
                Debug.LogWarning("Paste Vertex Data Failed.");
            }

            SkinnContext.Finished();
        }
    }
}